/*
 * BanBu_VoiceViewCell
 * @description 本文件提供聊天界面对话界面显示语音消息的cell
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供聊天界面对话界面显示语音消息的cell
 */

#import "BanBu_VoiceViewCell.h"
#import "AppDataManager.h"
#import "BanBuAPIs.h"
#import "BanBu_ChatCellAPI.h"
#import "AppCommunicationManager.h"

@implementation BanBu_VoiceViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        //图片
        _voiceView = [[BanBu_VoiceView alloc] initWithFrame:CGRectZero];
        [self addSubview:_voiceView];
        
        _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [_activityIndicator setFrame:CGRectMake(0, 17, 30, 30)];
        _activityIndicator.center = self.bkView.center;
        [_activityIndicator setHidesWhenStopped:YES];
        [_activityIndicator setColor:[UIColor blackColor]];
        
        [_activityIndicator  startAnimating];
        [self addSubview:_activityIndicator];
    }
    return self;
}

-(void)dealloc
{
    [_voiceView release],_voiceView = nil;
    [_activityIndicator release],_activityIndicator = nil;
    [super dealloc];
}

-(void)layoutSubviews
{
    //设置父类
    [super layoutSubviews];
    
    self.bkView.alpha = 1;
    _voiceView.frame = VoiceViewRect00;
    NSLog(@"_voiceView.frame:%@",NSStringFromCGRect(_voiceView.frame));
    _activityIndicator.center = self.bkView.center;
    
//    if (self.bubbleID == 0)
    {
        CGRect frame = _voiceView.frame;
        frame.origin.y += 3;
        if (!self.atLeft)
        {
            frame.origin.x += 3;
        }
        _voiceView.frame = frame;
    }

    //如果显示from
    if (self.showFrom)
    {
        [self setShowFromFrame:1];
    }
    if (self.atLeft)
    {
        self.statusView.frame = CGRectMake(StatusViewRect.origin.x + 3, StatusViewRect.origin.y - 2 , StatusViewRect.size.width/2, StatusViewRect.size.height/2);
    }
    
    self.avatar.frame = AvatarRect01;
//    [self.vipImageView setFrame:CGRectMake(self.avatar.frame.origin.x + 28, self.avatar.frame.origin.y + 26, 15, 10)];
}

//设置cell的各种信息
-(void)setChatCellInfo:(NSDictionary *)amsg
{
    self.bkViewWidth = 80.0;
    _voiceView.appChatController = (BanBu_ChatViewController *)self.delegate;
    
    //设置父类的属性
    [super setChatCellInfo:amsg];
    
    MediaStatus status = [VALUE(KeyMediaStatus, amsg) integerValue];
    NSString *content = VALUE(KeyContent, amsg);
    
    [_voiceView setStatus:status];
    [_voiceView setMedia:content];
    
    _voiceView.isLeft=self.atLeft;
    
    NSData *data=[NSData dataWithContentsOfFile:[AppComManager pathForMsgMedia:content]];
    
    if(data)
    {
        AVAudioPlayer *player=[[AVAudioPlayer alloc]initWithData:data error:nil];
        if (player.duration > 0.01)
        {
            [self setVoiceViewLong:player.duration];
        }
        //避免接收到的消息为0
        else
        {
            [self setVoiceViewLong:player.duration + 0.1];
        }
        [player release];
    }
    
    if(status == MediaStatusDownload)
    {
        [AppComManager getBanBuMedia:content forMsgID:VALUE(KeyChatid, amsg) fromUid:MyAppDataManager.chatuid delegate:MyAppDataManager];
    }
    
    [self setMsgMediastatus:status];
}

//设置语音气泡的长度
-(void)setVoiceViewLong:(CGFloat)time
{
    [_voiceView setTime:time];
    
    if(time<=5)
    {
        self.bkViewWidth = 80.0;
    }
    else if (time>5&&time<15)
    {
        self.bkViewWidth = 130.0;
    }
    else
    {
        self.bkViewWidth = 160.0;
    }
}

-(void)setMsgMediastatus:(MediaStatus)status
{
    if(status != MediaStatusNormal && status != MediaStatusReaded)//不是：已播过或正常时，loading转圈
    {
        [_activityIndicator  startAnimating];
    }
    else
    {
        [_activityIndicator  stopAnimating];
    }
}
@end
